<?php
namespace app\admin\controller\account;

use app\admin\BaseController;
use app\admin\model\ShopLevel;
use app\lib\exception\FailedException;
use app\Validate\IdMustPositive;
use app\Validate\PageMustValidate;
use app\Validate\ShopLevelValidate;
use think\Request;


class Level extends BaseController
{
    /**
     * 查旬所有商家等级
     * @param Request $request
     * @return \think\response\Json
     */
    public function levelList(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where = [];
        $where['page'] = $post['page'];
        $list = (new ShopLevel())->wechatListTotal($where,'id,level,min_price,max_price',"level",$post['page_number']);
        return writeJson(200,$list);
    }
    /**
     * 会员充值添加
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function levelAdd(Request $request){
        (new ShopLevelValidate())->goCheck();
        $post = $request->post();
        $new = [
            'level'=>$post['level'],
            'min_price'=>$post['min_price'],
            'max_price'=>$post['max_price'],
        ];
        $res = (new ShopLevel())->dataUpdate($new);
        if(!$res){
            throw new FailedException(['msg'=>'商家等级添加失败']);
        }
        return writeJson(200,[],'商家等级添加成功');

    }
    /**
     * 会员充值编辑
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function levelEdit(Request $request){
        (new IdMustPositive())->goCheck();
        (new ShopLevelValidate())->goCheck();
        $post = $request->post();
        $new = [
            'id'   =>$post['id'],
            'level'=>$post['level'],
            'min_price'=>$post['min_price'],
            'max_price'=>$post['max_price'],
        ];
        $res = (new ShopLevel())->dataUpdate($new);
        if(!$res){
            throw new FailedException(['msg'=>'商家等级编辑失败']);
        }
        return writeJson(200,[],'商家等级编辑成功');
    }
}